package com.sc.VenusQueryBeta.ui.fragments.account;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.fragment.BaseMVPCompatFragment;
import com.hengjun.kingstar.sdk.utils.LogUtil;
import com.hengjun.kingstar.sdk.utils.ScreenUtils;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.adapter.account.CurrencyAdapter;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.contract.account.CurrencyContract;
import com.sc.VenusQueryBeta.model.bean.account.CurrencyBean;
import com.sc.VenusQueryBeta.model.bean.login.QueryRegisBean;
import com.sc.VenusQueryBeta.presenter.account.CurrencyPresenter;
import com.sc.VenusQueryBeta.ui.fragments.account.changmian.ChangMianFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.cundan.CunDanFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.cunka.CunKaFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.daikuan.DaiKuanFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.daishu.DaiShuFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.xiadaikuan.XiaDaiKuanFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.xiaofei.XiaoFeiFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.xiazhuanma.XiaZhuanMaFragment;
import com.sc.VenusQueryBeta.ui.fragments.account.zhuanma.ZhuanMaFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.MaterialHeader;

public class CurrencyFragment extends BaseMVPCompatFragment<CurrencyContract.CurrencyPresenter> implements CurrencyContract.ICurrencyView {


    private static CurrencyFragment fragment;
    @BindView(R.id.about_vip_user_back)
    ImageButton aboutVipUserBack;
    @BindView(R.id.rv_currency_list)
    RecyclerView rvCurrencyList;
    @BindView(R.id.ptr_class)
    PtrClassicFrameLayout ptrClass;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    @BindView(R.id.tv_choose_currency)
    TextView tvChooseCurrency;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    private QueryRegisBean queryRegisBean;
    private int clientindex;
    private int queryType;
    private CurrencyAdapter currencyadapter;
    private boolean isUpdate;
    private String currency_type;

    public static CurrencyFragment newInstance(QueryRegisBean queryRegisBean, int clientindex, int queryType) {
        Bundle args = new Bundle();
        args.putInt(Constants.CLIENTINDEX, clientindex);
        args.putSerializable(Constants.QUERY_SETTING, queryRegisBean);
        args.putInt(Constants.QUERYTYPE, queryType);
        fragment = new CurrencyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initData() {
        super.initData();
        if (fragment.getArguments() != null) {
            queryRegisBean = (QueryRegisBean) fragment.getArguments().getSerializable(Constants.QUERY_SETTING);
            clientindex = fragment.getArguments().getInt(Constants.CLIENTINDEX, 0);
            queryType = fragment.getArguments().getInt(Constants.QUERYTYPE, 0);

        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        if (queryRegisBean != null && !queryRegisBean.getServerSetting().getServerAddress().equals("")) {
            //查询幣種
            mPresenter.getcurrency(queryRegisBean.getServerSetting().getServerAddress(), queryRegisBean.getServerSetting().getServerProject(),
                    queryRegisBean.getServerSetting().getServerPort(), clientindex, queryType);
        } else {
            ToastUtils.showToast("数据异常");
        }
    }

    @Override
    public boolean onBackPressedSupport() {
        pop();
        return true;
    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return CurrencyPresenter.newInstance();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_currency_fragment;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(rlTitle.getLayoutParams());

        lp.setMargins(0, ScreenUtils.getStatusBarHeight(mContext)+8, 0, 0);

        rlTitle.setLayoutParams(lp);
        currencyadapter = new CurrencyAdapter(R.layout.item_currency);
        rvCurrencyList.setAdapter(currencyadapter);
        rvCurrencyList.setLayoutManager(new LinearLayoutManager(mContext));
        showProgressDialog("請稍等...");
        reFreshListener();
    }


    @Override
    public void showNetworkError(Throwable throwable) {

    }

    @Override
    public void showgetCurrencyData(CurrencyBean currencyBean) {
        hideProgressDialog();
        if (currencyBean.getDatas() != null && currencyBean.getDatas().size() > 0) {
            if (ptrClass != null && tvEmpty != null) {
                LogUtil.e("sdads"+currencyBean.getDatas().get(2).getCommonValue());
                ptrClass.setVisibility(View.VISIBLE);
                tvEmpty.setVisibility(View.GONE);
                initRecycleView(currencyBean);
                LogUtil.e("queryType====" + queryType);
            }
        } else {
            if (ptrClass != null && tvEmpty != null) {
                tvEmpty.setVisibility(View.VISIBLE);
            }
        }
        ptrClass.refreshComplete();

    }


    @OnClick(R.id.about_vip_user_back)
    public void onViewClicked() {
        pop();
    }

    /**
     * 填充數據
     *
     * @param currencyBean
     */
    private void initRecycleView(final CurrencyBean currencyBean) {
        currencyadapter = new CurrencyAdapter(R.layout.item_currency, currencyBean.getDatas(), queryType);

        currencyadapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                currency_type = currencyBean.getDatas().get(position).getCurrencyName();
//                ToastUtils.showToast("sss="+currency_type);
                if (queryType == 0) {
                    start(ZhuanMaFragment.newInstance(queryRegisBean, clientindex, currency_type));
                } else if (queryType == 1) {
                    start(CunKaFragment.newInstance(queryRegisBean, clientindex, currency_type));//存卡
                } else if (queryType == 7) {
                    start(CunDanFragment.newInstance(queryRegisBean, clientindex, currency_type));//存單
                } else if (queryType == 2) {
                    start(XiaoFeiFragment.newInstance(queryRegisBean, clientindex, currency_type));//消費
                } else if (queryType == 3) {
                    start(DaiKuanFragment.newInstance(queryRegisBean, clientindex, currency_type));
                } else if (queryType == -4) {
                    start(DaiShuFragment.newInstance(queryRegisBean, clientindex, currency_type));
                } else if (queryType == 4) {
                    start(XiaZhuanMaFragment.newInstance(queryRegisBean, clientindex, currency_type));
                } else if (queryType == 5) {
                    start(XiaDaiKuanFragment.newInstance(queryRegisBean, clientindex, currency_type));
                } else if (queryType == 6) {
                    start(ChangMianFragment.newInstance(queryRegisBean, clientindex, currency_type));
                }
            }
        });
        rvCurrencyList.setAdapter(currencyadapter);
    }

    /**
     * 下拉刷新監聽
     */
    private void reFreshListener() {
        MaterialHeader header = new MaterialHeader(mActivity);
        ptrClass.addPtrUIHandler(header);
        ptrClass.setLastUpdateTimeRelateObject(this);//设置刷新支持时间
        ptrClass.setResistance(1.7f);//设置下拉的阻尼系数，值越大感觉越难下拉
        ptrClass.setDurationToCloseHeader(1000);//设置刷新完成，头部回弹时间，注意和前一个进行区别
        ptrClass.setPullToRefresh(false);//设置下拉过程中执行刷新，我们一般设置为false
        ptrClass.setKeepHeaderWhenRefresh(true);//设置刷新的时候是否保持头部
        ptrClass.setPtrHandler(new PtrDefaultHandler() {               //下拉刷新
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //模拟联网，延迟更新数据
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (ptrClass != null) {
                            isUpdate = true;
                            showProgressDialog("加载中...");
                            mPresenter.getcurrency(queryRegisBean.getServerSetting().getServerAddress(), queryRegisBean.getServerSetting().getServerProject(),
                                    queryRegisBean.getServerSetting().getServerPort(), clientindex, queryType);
                        } else {
                            hideProgressDialog();
                        }
                    }
                }, 1000);
            }
        });
    }
}
